package cn.admin.config.common;

import java.io.Serializable;

public class Revision implements Comparable<Revision>, Serializable {
    public static final Revision UNINITIALIZED_REVISION = new Revision(0L);
    private final long revision;

    public Revision(Revision revision) {
        this.revision = revision.revision;
    }

    public Revision(long revision) {
        this.revision = revision;
    }

    public synchronized Revision getUpdatedRevision() {
        return new Revision(this.revision + 1L);
    }

    @Override
    public int compareTo(Revision another) {
        if (this.getClass() == Revision.class && another.getClass() != Revision.class) {
            return -another.compareTo(this);
        } else {
            return Long.compare(this.revision, another.revision);
        }
    }

    public final boolean isNewerThan(Revision rev) {
        return this.compareTo(rev) > 0;
    }

    public final boolean isOlderThan(Revision rev) {
        return this.compareTo(rev) < 0;
    }

    @Override
    public int hashCode() {
        return (int)(this.revision ^ this.revision >>> 32);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (!(obj instanceof Revision)) {
            return false;
        } else {
            return this.compareTo((Revision)obj) == 0;
        }
    }

    @Override
    public String toString() {
        return String.valueOf(this.revision);
    }

    public long getRevision() {
        return this.revision;
    }
}
